<template>
  <div class="subject-container">
    <el-card shadow="never">
      <!-- 搜索栏 -->
      <el-row>
        <el-col :span="18">
          <el-form class="searchSubj">
            <el-form-item label="学科搜索">
              <el-input size="small" v-model="searchSubjectName" />
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="resetSearch()"> 清除 </el-button>
              <el-button
                size="small"
                type="primary"
                @click="getSubjectList(searchSubjectName)"
              >
                搜索
              </el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="success"
            size="small"
            @click="dialogUpdataSubject('新增学科')"
          >
            新增学科
          </el-button>
        </el-col>
      </el-row>
      <!-- 数据提示 -->
      <el-alert
        v-if="alertText !== ''"
        :title="alertText"
        type="info"
        class="alert"
        :closable="false"
        show-icon
        cell-class-name="table_cell"
      ></el-alert>
      <!-- 表格数据 -->
      <el-table :data="subjectList" fit size="medium">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="学科名称" prop="subjectName"> </el-table-column>
        <el-table-column label="创建者" prop="username"> </el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否前台显示">
          <template slot-scope="scope">
            <span>{{ scope.row.isFrontDisplay === 1 ? "是" : "否" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="二级目录" prop="twoLevelDirectory">
        </el-table-column>
        <el-table-column label="标签" prop="tags"> </el-table-column>
        <el-table-column label="题目数量" prop="totals"> </el-table-column>
        <el-table-column label="操作" class="rowButton" width="240">
          <!-- 操作按钮 -->
          <template slot-scope="scope">
            <el-button
              type="text"
              size="medium"
              @click="checkDirectorys(scope.row)"
            >
              学科分类
            </el-button>
            <el-button
              type="text"
              size="medium"
              @click="checkTagBtn(scope.row)"
            >
              学科标签
            </el-button>
            <el-button
              type="text"
              size="medium"
              @click="dialogUpdataSubject('修改学科', scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              size="medium"
              @click="deletesubject(scope.row.id)"
              :disabled="
                scope.row.twoLevelDirectory === 0 &&
                scope.row.tags === 0 &&
                scope.row.totals === 0
              "
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 20px; text-align: right"
        background
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 弹出对话框 -->
      <el-dialog :title="dialogtitle" :visible.sync="dialogVisible" width="30%" :before-close="handleDialogClose">
        <el-form
          :label-position="'right'"
          label-width="80px"
          :model="dialogForm"
          ref="dialogForm"
          :rules="dialogRules"
          :before-close="handleDialogClose"
        >
          <el-form-item label="学科名称" prop="subjectName">
            <el-input v-model="dialogForm.subjectName"></el-input>
          </el-form-item>
          <el-form-item label="是否显示" prop="isFrontDisplay">
            <el-switch
              v-model="dialogForm.isFrontDisplay"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-form-item size="large" style="text-align: right">
            <el-button @click="handleDialogClose">取消</el-button>
            <el-button type="primary" @click="submitForm">立即提交</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { parseTime } from '../../utils/index'
import { list, add, update, remove } from '../../api/hmmm/subjects'
export default {
  data () {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      subjectList: [],
      listLoading: true,
      searchSubjectName: '',
      alertText: '',
      dialogtitle: '',
      dialogVisible: false,
      labelPosition: 'right',
      diaologSwitch: true,
      dialogForm: {
        subjectName: '',
        isFrontDisplay: 1,
        id: null
      },
      dialogRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    diaologSwitch: {
      handler (newval) {
        newval
          ? (this.dialogForm.isFrontDisplay = 1)
          : (this.dialogForm.isFrontDisplay = 0)
      }
    }
  },
  created () {
    this.getSubjectList()
  },
  methods: {
    // 切换每页总数
    handleSizeChange (val) {
      //
      this.pageSize = val
      //
      this.getSubjectList()
    },
    // 切换当前页
    handleCurrentChange (val) {
      //
      this.currentPage = val
      this.getSubjectList()
    },
    // 获取学科列表数据
    async getSubjectList (name = '') {
      const res = await list({
        page: this.currentPage,
        pagesize: this.pageSize,
        subjectName: name
      })
      //
      res.data.items.forEach((item) => {
        // item.isFrontDisplay === 1
        //   ? (item.isFrontDisplay = '是')
        //   : (item.isFrontDisplay = '否')

        item.addDate = parseTime(item.addDate)
      })
      this.subjectList = res.data.items
      this.total = res.data.counts
      this.alertText = `数据一共 ${this.total} 条`
      //
    },
    // 显示对话框
    dialogUpdataSubject (title, obj = {}) {
      this.dialogtitle = title
      this.dialogForm.id = obj.id || null
      this.dialogForm.subjectName = obj.subjectName || null
      this.dialogForm.isFrontDisplay = obj.isFrontDisplay + ''
      this.dialogVisible = true
    },
    // 对话框隐藏
    handleDialogClose () {
      console.log(1112)
      this.$refs.dialogForm.resetFields()
      this.dialogVisible = false
    },
    // 提交表单
    submitForm () {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.submit() // 校验通过,提交表单
        } else {
          this.$message.error('请填写必填项')
        }
      })
    },
    // 提交
    async submit () {
      if (this.dialogForm.id == null) {
        this.dialogForm.isFrontDisplay = +this.dialogForm.isFrontDisplay
        //
        //
        await add(this.dialogForm)
        this.dialogVisible = false
        this.$refs.dialogForm.resetFields()
        this.getSubjectList()
      } else {
        this.dialogForm.isFrontDisplay = +this.dialogForm.isFrontDisplay
        //
        //
        await update(this.dialogForm)
        this.dialogVisible = false
        this.getSubjectList()
      }
    },
    // 重置搜索
    resetSearch () {
      this.searchSubjectName = ''
      this.getSubjectList()
    },
    // 点击学科分类按钮
    checkDirectorys (obj) {
      this.$router.push(`directorys?id=${obj.id}&name=${obj.subjectName}`)
    },
    // 点击学科标签按钮
    checkTagBtn (obj) {
      //
      this.$router.push(`tags?id=${obj.id}&name=${obj.subjectName}`)
    },
    // 删除学科消息框
    deletesubject (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getSubjectList()
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // })
        })
    }
  }
}
</script>

<style scoped lang="scss">
.subject-container {
  padding: 0 10px;
  margin: 10px 0;
}

.searchSubj {
  display: flex;
  align-items: center;
  .el-form-item {
    display: flex;
    align-items: center;
    margin: 0 10px 18px 0;
  }
}
.rowButton {
  width: 100%;
  display: flex;
}
.el-alert {
  margin: 0 0 15px;
}

.table_cell .cell {
  white-space: nowrap;
}
</style>
